package com.hotelsystem.reservation.controllers;

import com.hotelsystem.reservations.clientvalidation.ClientValidation;
import com.hotelsystem.reservations.layouts.OpenUserView;
import com.hotelsystem.reservations.persons.Client;
import com.hotelsystem.reservations.persons.ClientManagement;
import com.hotelsystem.reservations.persons.GenderEnum;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SaveUserController implements ActionListener{

    private OpenUserView view;

    public SaveUserController(OpenUserView v){
        this.view=v;
    }

    public void setOpenUserView(OpenUserView v){
        this.view = v;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //view.setClient(new Client());
        view.getClient().setFirstName(view.getTfFirstName().getText());
        view.getClient().setLastName(view.getTfLastName().getText());
        view.getClient().setLanguage(view.getTfLanguage().getText());
        view.getClient().setEmail(view.getTfEmail().getText());
        view.getClient().setAge(Integer.parseInt(view.getTfAge().getText()));
        view.getClient().setPersonalId(view.getTfPersonalId().getText());
        view.getClient().setTelephone(view.getTfTelephone().getText());
        if(view.getMale().isSelected())
            view.getClient().setGender(GenderEnum.MALE);
        else if(view.getFemale().isSelected())
            view.getClient().setGender(GenderEnum.FEMALE);
        view.getClient().setAdditionalBills(Double.parseDouble(view.getTfAdditionalBills().getText()));
        ClientValidation cv = new ClientValidation();
        String s = cv.validateClient(view.getClient());
        if(s.isEmpty()) {
            ClientManagement cm = new ClientManagement();
            cm.addClientInFile(view.getClient());
            view.getMainPanel().setVisible(false);
            view=null;
            JOptionPane.showMessageDialog(null, "User Added Successfuly!\n");
        }
        else {
            System.out.println("Wrong or incomplete data!\n" + s);
            JOptionPane.showMessageDialog(null,"Wrong or incomplete data!\n" + s);
        }
    }
}
